package vn.allmobile.ambnews.activities.category;

import vn.allmobile.ambnews.R;
import vn.allmobile.ambnews.network.LoadDataListener;
import vn.allmobile.ambnews.network.NetworkAPI;
import vn.allmobile.ambnews.network.NetworkChangeReceiver;
import vn.allmobile.ambnews.utils.Contants;
import vn.allmobile.ambnews.utils.GlobalApplication;
import vn.allmobile.ambnews.activities.list.ListActivity;
import vn.allmobile.ambnews.customview.LoadingDialog;
import vn.allmobile.ambnews.data.DbFeedAdapter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class CategoryItemClickListener implements OnClickListener,LoadDataListener  {
	
	
	String _categoryId;
	int _categoryIndex;
	LoadDataListener _listenner;	
	LoadingDialog _dialog;
	Boolean isCancel;
	Boolean _isUserPaper;
	Activity _mActivity;
	
	public Handler mHandle = new Handler()
	{
	  public void handleMessage(Message paramAnonymousMessage)
	  {
		  Context context = GlobalApplication._();
	    switch (paramAnonymousMessage.what)
	    {
	    	case 0:
	    	
	    		Intent itent = new Intent(context,ListActivity.class);
	    		//itent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	    		if(_isUserPaper)
	    		{
	    			itent.putExtra(Contants.EXTRA_LIST_TYPE, Contants.LIST_TYPE_USER);
	    		}	    		
	    		_mActivity.startActivity(itent);		    		
	    		_mActivity.overridePendingTransition(R.anim.slide_in_right,R.anim.no_animation);
				//_listenner.onListloadSuccess();
	    		break;
	    	case 1:
	    		Toast.makeText(GlobalApplication._(), "Không có mạng.",Toast.LENGTH_LONG).show();	
	    	break;	
	    }
	  }
	};
	
	public CategoryItemClickListener(LoadDataListener activity,Activity _mActivity, int itemIndex, String itemId,Boolean isUserPaper) {		
		//NetworkAPI._currentCategoryIndex = itemIndex;
		_categoryIndex = itemIndex;
		_categoryId	= itemId;
		_listenner = activity;
		_isUserPaper = isUserPaper;
		this._mActivity = _mActivity;
	}

	@Override
	public void onClick(View v) {
	
		isCancel = false;				
		NetworkAPI._currentCategoryId = _categoryId;
		NetworkAPI._currentCategoryIndex = _categoryIndex;
			
		NetworkAPI._listArticles = DbFeedAdapter.getListArticle(NetworkAPI._currentPaperId, _categoryId,false);
		
		if(NetworkChangeReceiver.isConnected(true))
		{
			this.mHandle.sendEmptyMessage(0);
		}
		else
		{			
			if(NetworkAPI._listArticles.size() > 0)
				this.mHandle.sendEmptyMessage(0);
			else
				this.mHandle.sendEmptyMessage(1);
					
		}

	}
	
	@Override
	public void onPaperloadSuccess() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onCategoryloadSuccess() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onListloadSuccess() {
		// TODO Auto-generated method stub
		this.mHandle.sendEmptyMessage(0);
		
		
	}

	@Override
	public void onListloadFail() {
		// TODO Auto-generated method stub
		this.mHandle.sendEmptyMessage(2);
	}

	@Override
	public void onUserloadSuccess() {
		// TODO Auto-generated method stub
		
	}

}
